package net.amsoft.iservice.isso.hibernate.tests;
import net.amsoft.iservice.isso.hibernate.Cookie;
import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.Response;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOHelpers;
import net.amsoft.iservice.util.HibernateUtil;
import net.amsoft.iservice.util.exception.IServiceException;

public class CookieTest
{
    private static int loopBroker = 0;

    protected static void makeSessionCookie(int sId) throws IServiceException {
        Cookie oCookie = new Cookie();
        oCookie.setEncCookieName("SESSIONCOOKIE");
        oCookie.setState((short) ISSOConstants.STATE_ACTIVE);
        oCookie.setTypeFlag(0);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oCookie.setPrincipal(oIB);
        }

        oCookie.insert();
        Cookie.listAll();

    }

    protected static void makeMachineCookie(int sId) throws IServiceException {

        Cookie oCookie = new Cookie();
        oCookie.setEncCookieName("MACHINECOOKIE");
        oCookie.setState((short) ISSOConstants.STATE_ACTIVE);
        oCookie.setTypeFlag(1);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oCookie.setPrincipal(oIB);

        }

        oCookie.insert();

    }

    protected static void makeResponse(int cookieId) throws IServiceException {

        Response oResponse = new Response();
        oResponse.setEncArtifactId("ENCARTIFACTID" + cookieId);
        oResponse.setResponse("RESPONSE" + cookieId);
        oResponse.setState((short) 1);
        if (cookieId > 0) {
            Cookie oCookie = (Cookie) ISSOHelpers.fetch(Cookie.class, new Long(
                    cookieId));
            oResponse.setCookie(oCookie);
        }
        oResponse.insert();

    }

    private static void updateSessionCookie() throws IServiceException {
        Cookie oCookie = (Cookie) ISSOHelpers.fetchCookieByName(
                "SESSIONCOOKIE", 0);
        oCookie.setState((short) 3);
        oCookie.update();

    }

    private static void updateMachineCookie() throws IServiceException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName("MACHINECOOKIE", 1);
        oCookie.setState((short) 3);
        oCookie.update();

    }

    private static void removeSessionCookie() throws IServiceException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName("MACHINECOOKIE", 1);
        if (oCookie != null)
            oCookie.remove();

    }

    private static void removeMachineCookie() throws IServiceException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName("MACHINECOOKIE", 1);
        if (oCookie != null)
            oCookie.remove();

    }

    public static void main(String[] args) throws IServiceException {
        //create ibroker
        IBrokerTest.mB();
        //create principal
        PrincipalTest.makePrincipal();
        //      create synonym
        PrincipalTest.makeSynonym(1, 0, "!!1001!1001");
        PrincipalTest.makeSynonym(1, 1, "=foo");

        PrincipalTest.makePrincipalMetaData(1);
        //Create Machine Cookie
        makeMachineCookie(1);
        //Create SessionCookie
        makeSessionCookie(1);
        makeResponse(0);
        makeResponse(1);
        updateSessionCookie();
        updateMachineCookie();
        removeMachineCookie();
        removeSessionCookie();

        HibernateUtil.getSessionFactory().close();
    }
}